package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Menu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
	@Autowired
	MenuDao menuDao;
	@Autowired
	UserDao userDao;

	@Override
	public PageResult findByPage(QueryPageBean queryPageBean) {
		log.debug("service:" + queryPageBean);
		//开始分页
		PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
		//条件查询
		Page<Menu> page = menuDao.findByCondition(queryPageBean.getQueryString());
		log.debug("page{" + page + "}");
		//封装PageResult
		return new PageResult(page.getTotal(), page);
	}

	@Override
	public void add(Menu menu) {
		menuDao.add(menu);
	}

	@Override
	public Menu findById(Integer id) {
		return menuDao.findById(id);
	}

	@Override
	public void edit(Menu menu) {
		//修改前判断自关联ID是否存在
		long count = menuDao.findCountByParentMenuId(menu.getParentMenuId());
		if (count == 0) {
			log.debug("当前菜单不存在,不能修改");
			throw new BusinessRuntimeException("当前自关联菜单不存在,不能修改");
		}
		menuDao.edit(menu);
	}

	@Override
	public void delById(Integer id) {
		//查询该菜单是否是一级菜单,一级菜单下是否有二级菜单,有,不能删除
		long count = menuDao.findCountByMenuId(id);
		if (count == 1) {
			//一级菜单,查询是否有子菜单
			long count2Menu = menuDao.findCountById(id);
			if (count2Menu > 0) {
				//有二级菜单,不能删除
				throw new BusinessRuntimeException("当前菜单有二级菜单,不能删除");
			}
			//查询菜单id是否和角色表有关联,有关联不能删除
			long countRoleMenu = menuDao.findCountByParentMenuId4RoleAndMenu(id);
			if (countRoleMenu > 0) {
				//有关联,不能删除
				throw new BusinessRuntimeException("当前菜单被引用,不能删除");
			}
			menuDao.delById(id);
			log.debug("删除成功");
		}
		if (count == 2) {
			//二级菜单
			//查询菜单id是否和角色表有关联,有关联不能删除
			long countRoleMenu = menuDao.findCountByParentMenuId4RoleAndMenu(id);
			if (countRoleMenu > 0) {
				//有关联,不能删除
				throw new BusinessRuntimeException("当前菜单被引用,不能删除");
			}
			menuDao.delById(id);
			log.debug("删除成功");
		}
	}

	@Override
	public List<Menu> findAll() {
		return menuDao.findAll();
	}

	@Override
	public List<Menu> showUserMenu(String username) {
		return menuDao.showUserMenu(userDao.findByUsername(username).getId());

	}

	@Override
	public List<Map> findMenuparentMenuId() {
		List list = new ArrayList<>();
		Map map = new HashMap<>();
		map.put("value", null);
		map.put("label", "一级菜单");
		list.add(map);
		ArrayList<Menu> menus = menuDao.findMenuparentMenuId();
		for (Menu menu : menus) {
			Integer id = menu.getId();
			Map mapc = new HashMap<>();
			mapc.put("value", id);
			mapc.put("label", menu.getName());
			if (menu.getParentMenuId() == null) {
				List<Menu> children = menu.getChildren();
				if (children.size() > 0) {
					findChildren(mapc, children, id);
				}
			}
			list.add(mapc);
		}
		return list;
	}

	public List findChildren(Map map, List<Menu> children, Integer id) {
		List clits = new ArrayList<>();
		for (Menu child : children) {
			Map mapc = new HashMap();
			mapc.put("value", child.getId());
			mapc.put("label", child.getName());
			if (child.getChildren().size() != 0) {
				List m = findChildren(mapc, child.getChildren(), id);
				mapc.put("children", m);
			}
			clits.add(mapc);
			id++;
		}
		map.put("children", clits);
		return clits;
	}

}

